package com.ruoyi.customer.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.customer.domain.CsActivity;
import com.ruoyi.customer.service.ICsActivityService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 活动管理Controller
 * 
 * @author 李守壮
 * @date 2023-03-22
 */
@RestController
@RequestMapping("/customer/activity")
public class CsActivityController extends BaseController
{
    @Autowired
    private ICsActivityService csActivityService;

    /**
     * 查询活动管理列表
     */
    @PreAuthorize("@ss.hasPermi('customer:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(CsActivity csActivity)
    {
        startPage();
        List<CsActivity> list = csActivityService.selectCsActivityList(csActivity);
        return getDataTable(list);
    }

    /**
     * 导出活动管理列表
     */
    @PreAuthorize("@ss.hasPermi('customer:activity:export')")
    @Log(title = "活动管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CsActivity csActivity)
    {
        List<CsActivity> list = csActivityService.selectCsActivityList(csActivity);
        ExcelUtil<CsActivity> util = new ExcelUtil<CsActivity>(CsActivity.class);
        util.exportExcel(response, list, "活动管理数据");
    }

    /**
     * 获取活动管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('customer:activity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(csActivityService.selectCsActivityById(id));
    }

    /**
     * 新增活动管理
     */
    @PreAuthorize("@ss.hasPermi('customer:activity:add')")
    @Log(title = "活动管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CsActivity csActivity)
    {
        Date endDate = csActivity.getEndTime();
        Date startDate = csActivity.getStartTime();
        int compareTo = endDate.compareTo(startDate);
        if (-1 == compareTo){
            return error("结束时间不能早于开始时间");
        }
        csActivity.setIsDown("N");
        return toAjax(csActivityService.insertCsActivity(csActivity));
    }

    /**
     * 修改活动管理
     */
    @PreAuthorize("@ss.hasPermi('customer:activity:edit')")
    @Log(title = "活动管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CsActivity csActivity)
    {
        return toAjax(csActivityService.updateCsActivity(csActivity));
    }

    /**
     * 删除活动管理
     */
    @PreAuthorize("@ss.hasPermi('customer:activity:remove')")
    @Log(title = "活动管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(csActivityService.deleteCsActivityByIds(ids));
    }


    /**
     * 导入
     */
    @Log(title = "CsActivity", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('customer:activity:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<CsActivity> util = new ExcelUtil<CsActivity>(CsActivity.class);
        List<CsActivity> list = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = csActivityService.importData(list, updateSupport, operName);
        return success(message);
    }
    /**
     * 导入模版
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<CsActivity> util = new ExcelUtil<CsActivity>(CsActivity.class);
        util.importTemplateExcel(response, "数据");
    }

}
